package com.corn.juc.demo;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * <p>
 * 题目：多线程之间按顺序调用，实现 A-> B -> C 三个线程启动，要求如下：
 * AA打印5次，BB打印10次，CC打印15次
 * 紧接着
 * AA打印5次，BB打印10次，CC打印15次
 * ..
 * 来10轮
 * @since : 2020/12/31 17:41
 */

public class LockConditionDemo {

    // 1->A线程 2->B线程 3->C线程
    private volatile int threadNo = 1;

    Lock lock = new ReentrantLock();

    Condition condition1 = lock.newCondition();

    Condition condition2 = lock.newCondition();

    Condition condition3 = lock.newCondition();

    Condition condition4 = lock.newCondition();

    AtomicInteger num = new AtomicInteger();

    private void printNumber(int time, Condition condition) throws Exception {
        try {
            lock.lock();
            while (threadNo != 1 && condition == condition1) {
                condition1.await();
            }
            while (threadNo != 2 && condition == condition2) {
                condition2.await();
            }
            while (threadNo != 3 && condition == condition3) {
                condition3.await();
            }
            while (threadNo != 4 && condition == condition4) {
                condition4.await();
            }
            if (time != 0) {
                for (int i = 0; i < time; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + num.incrementAndGet());
                }
            } else {
                System.out.println();
                System.out.println();
            }
            if (time == 5) {
                threadNo = 2;
                condition2.signal();
            } else if (time == 10) {
                threadNo = 3;
                condition3.signal();
            } else if (time == 15) {
                threadNo = 4;
                condition4.signal();
            } else {
                threadNo = 1;
                condition1.signal();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        LockConditionDemo demo = new LockConditionDemo();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    demo.printNumber(5, demo.condition1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "A").start();
            new Thread(() -> {
                try {
                    demo.printNumber(10, demo.condition2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "B").start();
            new Thread(() -> {
                try {
                    demo.printNumber(15, demo.condition3);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "C").start();
            new Thread(() -> {
                try {
                    demo.printNumber(0, demo.condition4);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }
}
